package com.example.javalib.jihe;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

public class ComparatorTest {
    public static void main(String[] args) {
        final ArrayList<String> strs = new ArrayList<>();
        strs.add("1");
        strs.add("9");
        strs.add("6");
        stringCompare(strs);
        System.out.println("================================================");
        final ArrayList<Integer> ints = new ArrayList<>();
        ints.add(1);
        ints.add(9);
        ints.add(3);
        ints.add(6);
        intCompare(ints);

    }
    static void stringCompare(final ArrayList<String> strs){

        Collections.sort(strs, new Comparator<String>() {
            @Override
            public int compare(String t1, String t2) {
//                System.out.println(strs);
//                升序排列
                return t1.compareTo(t2);
            }
        });
        System.out.println("升序排列："+strs);

        Collections.sort(strs, new Comparator<String>() {
            @Override
            public int compare(String t1, String t2) {
//                System.out.println(strs);
//                降序排列
                return t2.compareTo(t1);
            }
        });
        System.out.println("降序排列："+strs);

    }

    static void intCompare(final ArrayList<Integer> ints){

        Collections.sort(ints, new Comparator<Integer>() {
            @Override
            public int compare(Integer t1, Integer t2) {
                System.out.print(ints);
                System.out.print(t1+"  "+t2+" ");

//                升序排列
/*              t1：位于左侧
                t2：位于右侧
                升序就是要左边小，右边大
                若t1大于t2，就换位，
                否则不换位*/
                int result=t1-t2;
                if (result>0) {
                    System.out.print("换位");
                }else if (result==0){
                    System.out.print("不换位");
                }else if (result<0){
                    System.out.print("不换位");
                }
                System.out.println();
                return result;
            }
        });
        System.out.println("升序排列："+ints);

        Collections.sort(ints, new Comparator<Integer>() {
            @Override
            public int compare(Integer t1, Integer t2) {
//                System.out.println(strs);
//                降序排列
                return t2-t1;
            }
        });
        System.out.println("降序排列："+ints);

    }
}
